package screen;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import main.GameCanvas;
import main.NuoiThuMidlet;
import model.Command;
import model.IAction;
import model.PlayerInfo;
import service.GlobalService;
import utilities.GameResource;
import utilities.TField;
import font.BitmapFont;

public class ProfileScr extends Screen {

    static ProfileScr instance;
    private PlayerInfo m_playerInfo;

    public static ProfileScr gI() {
        if (instance == null) {
            instance = new ProfileScr();
        }
        return instance;
    }
    //name, level, money, gender
    private TField listTextField[];
    //private String listText[] = {GameResource.fullName, GameResource.blast, GameResource.mobile, GameResource.gender ,GameResource.avatar};
    private String listText[] = {GameResource.blast, GameResource.mobile, GameResource.gender ,GameResource.avatar};
    
    int wC, xC;
    Command cmdClose;
    Command cmdChangeAvatar;
    Command cmdFindFriend;
    Command cmdSelect1;
    boolean flagChangeAvatar = false;
    public Screen lastScr;

    //show screen
    public void switchToMe(PlayerInfo player) {
        m_playerInfo = player;
        lastScr = GameCanvas.currentScreen;
        m_cmdRight = listTextField[0].cmdClear;
        m_cmdLeft = cmdClose;
        m_cmdCenter = cmdFindFriend;
        
        /*
        listTextField[0].setText(player.fullName);
        listTextField[1].setText(player.status);
        listTextField[2].setText(player.mobile);
        listTextField[3].setText(player.gender == 1 ? GameResource.female : GameResource.male);
          */

        listTextField[0].setText(player.status);
        listTextField[1].setText(player.mobile);
        listTextField[2].setText(player.gender == 1 ? GameResource.female : GameResource.male);
        
       
        m_selected = 0;
        for (int i = 0; i < listTextField.length; i++) {
            listTextField[i].isFocus = false;
        }
        listTextField[0].isFocus = true;
        super.switchToMe();
    }

    public ProfileScr() {
        m_width = GameCanvas.w3d4;
        m_left = (GameCanvas.w - m_width) / 2;

        m_height = 8 * (ITEM_HEIGHT + 2) + 2 * 5 + 30;
        m_top = (GameCanvas.h - m_height) / 2;

        wC = m_width - 100;
        xC = m_left + 70;

        int y = m_top + 15 + (ITEM_HEIGHT + 2) / 2;
        listTextField = new TField[3];
        for (int i = 0; i < listTextField.length; i++) {
            listTextField[i] = new TField();
            listTextField[i].y = y;
            listTextField[i].x = xC;
            
            listTextField[i].width = wC;
            listTextField[i].height = ITEM_HEIGHT + 2;
            if (i == 2) {
                listTextField[i].setIputType(TField.INPUT_TYPE_NUMERIC);
            } else {
                listTextField[i].setIputType(TField.INPUT_TYPE_ANY);
            }
            listTextField[i].isFocus = false;
            y += ITEM_HEIGHT + 4;
        }
        
        listTextField[0].isFocus = true;
       /*
        listTextField[0].setMaxTextLenght(25);
        listTextField[0].setIputType(TField.INPUT_ALPHA_NUMBER_ONLY);
        */
        listTextField[0].setMaxTextLenght(40);
        
        listTextField[1].setMaxTextLenght(11);
        listTextField[1].setIputType(TField.INPUT_TYPE_NUMERIC);
        
        listTextField[2].isReadonly = true;
        
        
        cmdFindFriend = new Command(GameResource.changeInfo, new IAction() {

            public void perform() {
               // final String fullName = listTextField[0].getText().trim();
                final String status = listTextField[0].getText().trim();
                final String mobile = listTextField[1].getText().trim();
                final int gender = GameResource.female.equals(listTextField[2].getText().trim()) ? 1 : 0;
                //final int addfriendStatus = GameResource.inaccept.equals(listTextField[4].getText().trim()) ? 1 : 0;
                
                if (m_playerInfo.status.equals(status)
                        && m_playerInfo.mobile.equals(mobile) && (m_playerInfo.gender == gender)) {
                    GameCanvas.startOKDlg("Không có thông tin nào thay đổi");
                    return;
                }
               
                if(mobile.length() > 0){
                	try{
                		Integer.parseInt(mobile);
                	}catch(Exception ex){
                		GameCanvas.startOKDlg("Số mobile không hợp lệ");	
                		return;
                	}
                }
                
                GameCanvas.startOKDlg("Bạn thực sự muốn thay đổi thông tin cá nhân", new IAction() {

                    public void perform() {
                        GlobalService.gI().doUpdateProfile("", status, mobile, gender);
                        close();
                    }
                });
            }
        });

        cmdClose = new Command(GameResource.close, new IAction() {

            public void perform() {
                flagChangeAvatar = false;
                close();
            }
        });
        cmdChangeAvatar = new Command(GameResource.changeAvatar, new IAction(){
            
            public void perform() {
                 GameCanvas.startOKDlg("Bạn sẽ vào kho avatar và chọn avatar để thay?", new IAction() {
                     public void perform(){
                         flagChangeAvatar = true;
                         close();
                         GameCanvas.endDlg();
                         MainScr.gI().goMyStock(4);
                     }
                 });
                
            }
        }); 

        cmdSelect1 = new Command(GameResource.select, new IAction() {

            public void perform() {
                // TODO Auto-generated method stub
                Vector menu = new Vector();
                Command maleCmd = new Command(GameResource.male, new IAction() {

                    public void perform() {
                        // TODO Auto-generated method stub
                        listTextField[2].setText(GameResource.male);
                    }
                });
                menu.addElement(maleCmd);
                
               Command femaleCmd = new Command(GameResource.female, new IAction() {

                    public void perform() {
                        // TODO Auto-generated method stub
                        listTextField[2].setText(GameResource.female);
                    }
                });
                menu.addElement(femaleCmd);
                GameCanvas.menu.startAt(menu, 1);
            }
        });
    }

    public void close() {
        m_cmdCenter = null;
        m_cmdRight = null;
        m_cmdLeft = null;
        GameCanvas.cameraList.close();

        lastScr.switchToMe();
        
    }

    public void update() {
        lastScr.update();
        for (int i = 0; i < listTextField.length; i++) {
        	listTextField[i].update();
        }
    }

    public void updateKey() {
    	boolean checkFocus = false;
        if (GameCanvas.isPointerClick) {
            for (int i = 0; i < listTextField.length; i++) {
                TField txt = listTextField[i];
                if (GameCanvas.isPointer(txt.x, txt.y, txt.width, txt.height)) {
                	m_selected = i;
                    txt.isFocus = true;
                    if(i ==4){
                        m_cmdRight = cmdChangeAvatar;
                    }
                    else if (i == 2) {
                        m_cmdRight = cmdSelect1;
                        txt.lockArrow = true;
                    }else{
                    	m_cmdRight = txt.cmdClear;
                    }
                    
                    checkFocus = true;
                    break;
                }
            }
        }


        if (GameCanvas.keyPressed[2]) {
        	listTextField[m_selected].isFocus = false;
        	m_selected--;
            if (m_selected < 0) {
            	m_selected = listTextField.length - 1;
            }
            listTextField[m_selected].isFocus = true;
        } else if (GameCanvas.keyPressed[8]) {
        	listTextField[m_selected].isFocus = false;
        	m_selected++;
            if (m_selected >= listTextField.length) {
            	m_selected = 0;
            }
            listTextField[m_selected].isFocus = true;
        }
        
        if (GameCanvas.keyPressed[2] || GameCanvas.keyPressed[8]) {
            if(m_selected == 4){
                m_cmdRight = cmdChangeAvatar;
            }
            else if (m_selected == 2) {
                m_cmdRight = cmdSelect1;
            } else{
                m_cmdRight = listTextField[m_selected].cmdClear;
            }
            
            GameCanvas.keyPressed[2] = false;
            GameCanvas.keyPressed[8] = false;
        }
        
        if(checkFocus){
        	for (int i = 0; i < listTextField.length; i++) {
                TField txt = listTextField[i];
                if (GameCanvas.isPointer(txt.x, txt.y, txt.width, txt.height)) {
                    txt.isFocus = true;
                }
                else
                	txt.isFocus = false;
            }
        }
        
        super.updateKey();
    }

    public void keyPress(int keyCode) {
        int a = listTextField.length - 1;
        for (int i = 0; i < a; i++) {
            if (listTextField[i].isFocus && !listTextField[i].isReadonly) {
                listTextField[i].keyPressed(keyCode);
            }
        }

        super.keyPress(keyCode);
    }

    public void paint(Graphics g) {
        lastScr.paint(g);
        
        g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
        
        PaintPopup.paintRoundRect(g, m_left, m_top, m_width, m_height, 20, 0x3c8dc0);
        int y = m_top + 10;

        BitmapFont.drawBoldFont(g, GameResource.info, m_left + m_width / 2, y, 0xffffff, Graphics.HCENTER);
        
        int i;
        for (i = 0; i < listTextField.length; i++) {
            BitmapFont.drawNormalFont(g, listText[i], m_left + 10, listTextField[i].y + 4, 0x3a001e, Graphics.LEFT);
        }
        BitmapFont.drawNormalFont(g, listText[i], m_left + 10, listTextField[i - 1].y + 4 + ITEM_HEIGHT, 0x3a001e, Graphics.LEFT);
        
        for (i = 0; i < listTextField.length; i++) {
            listTextField[i].paint(g);
        }

        int xInfo = m_left + 10;
        y = listTextField[listTextField.length - 1].y;
        y += 40;
        
        g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
        
        PaintPopup.paintBox(g, listTextField[0].x, y - 15, 32, 32, 1, 0x51659e, 0x2b6589);
		NuoiThuMidlet.m_myPlayerInfo.m_defaultAvatar.paintIcon(g, listTextField[0].x, y - 15);
		
        y += 25;
        BitmapFont.drawNormalFont(g, GameResource.exp + ": ", xInfo, y, 0x3a001e, Graphics.LEFT);
        BitmapFont.drawBoldFont(g, "" + m_playerInfo.m_sExperience, xInfo+75, y, 0x3a001e, Graphics.LEFT);
        
        super.paint(g);
    }
    public void paintMarqueeMsg(Graphics g){
    	
	}
}
